package org.xiaodongwei.jvm.utils;

public class ObjectUtils {

    public static boolean isInteger(Object o) {
        return o instanceof Integer;
    }

    public static boolean isLong(Object o) {
        return o instanceof Long;
    }

    public static boolean isFloat(Object o) {
        return o instanceof Float;
    }

    public static Integer toInteger(Object o) {
        if (isInteger(o)) {
            return (Integer) o;
        }
        throw new ClassCastException(o + ": [ObjectUtils.toInteger] Type error. Cannot be converted");
    }

    public static Long toLong(Object o) {
        if (isLong(o)) {
            return (Long) o;
        }
        throw new ClassCastException(o + ": [ObjectUtils.toLong] Type error. Cannot be converted");
    }

    public static Float toFloat(Object o) {
        if (isFloat(o)) {
            return (Float) o;
        }
        throw new ClassCastException(o + ": [ObjectUtils.toFloat] Type error. Cannot be converted");
    }

    public static boolean isSameType(Object o1, Object o2) {
        return o1.getClass() == o2.getClass();
    }

    public static boolean isSafeCompareCode(Object o) {
        if (!isInteger(o)) return false;
        int code = toInteger(o);
        return code == 0 || code == -1 || code == 1;
    }
}
